# -*- coding:utf8 -*-
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'Pixel 2 API 23'
desired_caps['newCommandTimeout'] = 8000
# desired_caps['automationName'] = 'UIAutomator2'
desired_caps['noReset'] = True
#desired_caps['app'] = '/Users/lichongyang/program/rearch/AndroidCoverage/apk/OpenSudoku.apk'
#desired_caps['app'] = '/Users/nju/Desktop/apk/apk/com.tomaszmarzeion.notepad_1.apk'

desired_caps['appPackage'] = 'ru.andrey.notepad'
desired_caps['appActivity']= '.MainActivity'

desired_caps["unicodeKeyboard"] = True
desired_caps["resetKeyboard"] = True
#desired_caps['appWaitActivity'] = 'com.ichi2.anki.DeckPicker'

# test case1: 点击添加按钮，创建卡组，取消
#driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(20)

time.sleep(5)

# 注：
# 1、该应用添加笔记时，如果需要对文字样式进行修改，需要选中文字，未测试
# 2、画笔时滑动操作，未测试

# test case1: 添加笔记
el = driver.find_element_by_id("ru.andrey.notepad:id/addbtn")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.TextView")[1]
el.click()

time.sleep(2)

el = driver.find_element_by_id("ru.andrey.notepad:id/editText1")
el.send_keys("Test")

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[1]
el.click()

time.sleep(2)

el = driver.find_element_by_id("android:id/button1")
el.click()

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[2]
el.click()

time.sleep(2)

driver.back()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button1")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button2")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button3")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button4")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button5")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button6")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button7")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button8")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button9")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[3]
el.click()

el = driver.find_element_by_id("ru.andrey.notepad:id/button1")
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[0]
el.click()

time.sleep(2)

# test case2: 再添加一个笔记，不添加任何内容，直接存储
el = driver.find_element_by_id("ru.andrey.notepad:id/addbtn")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.TextView")[1]
el.click()

time.sleep(2)

el = driver.find_element_by_accessibility_id("More options")
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.LinearLayout")[0]
el.click()

time.sleep(2)

# test case3: 主页面，搜索
el = driver.find_element_by_id("ru.andrey.notepad:id/editText1")
el.send_keys("Test")

time.sleep(2)

el = driver.find_element_by_id("ru.andrey.notepad:id/editText1")
el.clear()

# test case4: 点击第一个笔记进入，然后返回
el = driver.find_elements_by_class_name("android.widget.TextView")[2]
el.click()

time.sleep(2)

driver.back()

# test case5: 重命名笔记
el = driver.find_elements_by_id("ru.andrey.notepad:id/imageView3")[0]
el.click()

time.sleep(2)

el = driver.find_elements_by_class_name("android.widget.EditText")[0]
el.clear()

el = driver.find_elements_by_class_name("android.widget.EditText")[0]
el.send_keys("Test1")

el = driver.find_element_by_id("android:id/button1")
el.click()

# test case6: 删除笔记
el = driver.find_elements_by_id("ru.andrey.notepad:id/imageView2")[0]
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

time.sleep(2)

el = driver.find_elements_by_id("ru.andrey.notepad:id/imageView2")[0]
el.click()

el = driver.find_element_by_id("android:id/button1")
el.click()

time.sleep(2)
# test case7: 退出
driver.back()

el = driver.find_element_by_id("ru.andrey.notepad:id/button1")
el.click()

driver.quit()

